//
//  InviteRewardUtils.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/10/21.
//

import Foundation
class InviteRewardUtils {
    static func doTest1() -> InviteRewardBean {
        let bean = InviteRewardBean()
        bean.objectId = UUID().uuidString
        bean.rewardUserId = userManager.getUserId()
        bean.avatar = userManager.getAvatar()
        bean.name = "我"
        bean.content = "完成绑定"
        bean.count = configManager.getConfig().aiChat.inviteCount
        bean.test = true
        return bean
    }
    static func doTest2() -> InviteRewardBean {
        let bean = InviteRewardBean()
        bean.objectId = UUID().uuidString
        bean.rewardUserId = userManager.getUserId()
        bean.avatar = "http://cdn.aiyuxm.com/imprint/ai/invite/example_1.jpeg"
        bean.name = "月儿月"
        bean.content = "已通过你的邀请码完成绑定"
        bean.count = configManager.getConfig().aiChat.inviteCount
        bean.test = true
        return bean
    }
    static func doTest3() -> InviteRewardBean {
        let bean = InviteRewardBean()
        bean.objectId = UUID().uuidString
        bean.rewardUserId = userManager.getUserId()
        bean.avatar = "http://cdn.aiyuxm.com/imprint/ai/invite/example_2.jpeg"
        bean.name = "**夏"
        bean.content = "已升级年度Pro"
        bean.count = 50
        bean.test = true
        return bean
    }
    static func doTest4() -> InviteRewardBean {
        let bean = InviteRewardBean()
        bean.objectId = UUID().uuidString
        bean.rewardUserId = userManager.getUserId()
        bean.avatar = "http://cdn.aiyuxm.com/imprint/ai/invite/example_1.jpeg"
        bean.name = "月儿月"
        bean.content = "已升级永久Pro"
        bean.count = 100
        bean.test = true
        return bean
    }
    static func doTest5() -> InviteRewardBean {
        let bean = InviteRewardBean()
        bean.objectId = UUID().uuidString
        bean.rewardUserId = userManager.getUserId()
        bean.avatar = "http://cdn.aiyuxm.com/imprint/ai/invite/example_3.jpeg"
        bean.name = "*****柠"
        bean.content = "已升级永久Pro（限时）"
        bean.count = 1000
        bean.test = true
        return bean
    }
    static func doTest6() -> InviteRewardBean {
        let bean = InviteRewardBean()
        bean.objectId = UUID().uuidString
        bean.rewardUserId = userManager.getUserId()
        bean.avatar = "http://cdn.aiyuxm.com/imprint/ai/invite/example_3.jpeg"
        bean.name = "*****柠"
        bean.content = "已购买1000回应次数"
        bean.count = 50
        bean.test = true
        return bean
    }
    static func doTest7() -> InviteRewardBean {
        let bean = InviteRewardBean()
        bean.objectId = UUID().uuidString
        bean.rewardUserId = userManager.getUserId()
        bean.avatar = "http://cdn.aiyuxm.com/imprint/ai/invite/example_4.jpeg"
        bean.name = "*****笑"
        bean.count = 500
        bean.content = "已购买1w回应次数"
        bean.reward = true
        bean.test = true
        return bean
    }
    
    //创建自己的奖励
    static func doCreateBind() async throws {
        let bean = InviteRewardBean()
        bean.rewardUserId = userManager.getUserId()
        bean.userId = userManager.getUserId()
        bean.avatar = userManager.getAvatar()
        bean.name = "我"
        bean.content = "完成绑定"
        bean.count = configManager.getConfig().aiChat.inviteCount
        bean.qq = userManager.getUser()?.qq
        bean.wx = userManager.getUser()?.wx
        bean.phone = userManager.getUser()?.phone
        bean.apple = userManager.getUser()?.apple
        try await retry(maxRetries: 10, retryDelayMillis: 100) {
            try await LeancloudManager.instance.save(todo: bean)
        }
    }
    
    //创建对方的奖励
    static func doCreateBind(toUserId: String) async throws {
        let bean = InviteRewardBean()
        bean.rewardUserId = toUserId
        bean.userId = userManager.getUserId()
        bean.avatar = userManager.getAvatar()
        bean.name = userManager.getName()
        bean.content = "已通过你的邀请码完成绑定"
        bean.count = configManager.getConfig().aiChat.inviteCount
        bean.qq = userManager.getUser()?.qq
        bean.wx = userManager.getUser()?.wx
        bean.phone = userManager.getUser()?.phone
        bean.apple = userManager.getUser()?.apple
        try await retry(maxRetries: 10, retryDelayMillis: 100) {
            try await LeancloudManager.instance.save(todo: bean)
        }
    }
    
    //升级会员
    static func doUpdatePro(_ toUserId: String?,_ text: String,_ count: Float) async throws {
        if ((toUserId ?? "").isEmpty == true) {
            return
        }
        if (count <= 0) {
            return
        }
        let bean = InviteRewardBean()
        bean.rewardUserId = toUserId
        bean.userId = userManager.getUserId()
        bean.avatar = userManager.getAvatar()
        bean.name = userManager.getName()
        bean.content = "已升级\(text)"
        bean.count = count
        bean.qq = userManager.getUser()?.qq
        bean.wx = userManager.getUser()?.wx
        bean.phone = userManager.getUser()?.phone
        bean.apple = userManager.getUser()?.apple
        try await retry(maxRetries: 10, retryDelayMillis: 100) {
            try await LeancloudManager.instance.save(todo: bean)
        }
    }
    
    //购买次数
    static func doBuyCount(_ toUserId: String?, _ text: String,_  count: Float) async throws {
        if ((toUserId ?? "").isEmpty == true) {
            return
        }
        if (count <= 0) {
            return
        }
        let bean = InviteRewardBean()
        bean.rewardUserId = toUserId
        bean.userId = userManager.getUserId()
        bean.avatar = userManager.getAvatar()
        bean.name = userManager.getName()
        bean.content = "已购买\(text)"
        bean.count = count
        bean.qq = userManager.getUser()?.qq
        bean.wx = userManager.getUser()?.wx
        bean.phone = userManager.getUser()?.phone
        bean.apple = userManager.getUser()?.apple
        try await retry(maxRetries: 10, retryDelayMillis: 100) {
            try await LeancloudManager.instance.save(todo: bean)
        }
    }
}
